# -*- python -*-
# ex: set syntax=python:

# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from buildbot.changes.filter import ChangeFilter
from buildbot.scheduler import Nightly
from buildbot.schedulers.basic import SingleBranchScheduler

from common import chromium_utils

from master import gitiles_poller
from master import master_utils
from master import slaves_list
from master.factory import annotator_factory

import branches_cfg
import config
import master_site_config


import os
import re

# Get the branches currently tracked on the waterfall, plus the names for the
# slaves on these branches from the branches_cfg.py file.
stable_branch = branches_cfg.stable_branch
beta_branch = branches_cfg.beta_branch

ActiveMaster = master_site_config.V8Branches

MAIL_NOTIFIER = ActiveMaster.is_production_host

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}
c['status'] = []


####### DATABASE

config.DatabaseSetup(c, require_dbconfig=ActiveMaster.is_production_host)

####### CHANGESOURCES

comparator = gitiles_poller.GitilesRevisionComparator()
c['change_source'] = [
  gitiles_poller.GitilesPoller(
      'https://chromium.googlesource.com/v8/v8',
      branches=[
        'refs/branch-heads/%s' % stable_branch,
        'refs/branch-heads/%s' % beta_branch,
      ],
      pollInterval=10,
      comparator=comparator,
  ),
  gitiles_poller.GitilesPoller(
      'https://chromium.googlesource.com/v8/v8',
      branches=[re.compile(r'refs/heads/\d+\.\d+\.\d+')],
      pollInterval=20,
      category='roll',
      comparator=comparator,
  )
]

####### SCHEDULERS

## configure the Schedulers
s_v8_roll = SingleBranchScheduler(
    name='v8_roll',
    change_filter=ChangeFilter(category='roll'),
    treeStableTimer=10,
    builderNames=[
        'V8 Linux - roll branch',
        'V8 Linux - roll branch - debug',
        'V8 arm - sim - roll branch',
        'V8 arm - sim - roll branch - debug',
        'V8 Linux64 - roll branch',
        'V8 Linux64 - roll branch - debug'],
)

s_v8_stable = SingleBranchScheduler(
    name='v8_stable',
    branch=stable_branch,
    treeStableTimer=10,
    builderNames=[
        'V8 Linux - stable branch',
        'V8 Linux - stable branch - debug',
        'V8 Linux64 - stable branch',
        'V8 Linux64 - stable branch - debug',
        'V8 arm - sim - stable branch',
        'V8 arm - sim - stable branch - debug',
        'V8 mipsel - sim - stable branch',
        'V8 mips64el - sim - stable branch',
        'V8 ppc - sim - stable branch',
        'V8 ppc64 - sim - stable branch'],
)

s_v8_beta = SingleBranchScheduler(
    name='v8_beta',
    branch=beta_branch,
    treeStableTimer=10,
    builderNames=[
          'V8 Linux - beta branch',
          'V8 Linux - beta branch - debug',
          'V8 Linux64 - beta branch',
          'V8 Linux64 - beta branch - debug',
          'V8 arm - sim - beta branch',
          'V8 arm - sim - beta branch - debug',
          'V8 mipsel - sim - beta branch',
          'V8 mips64el - sim - beta branch',
          'V8 ppc - sim - beta branch',
          'V8 ppc64 - sim - beta branch'],
)

c['schedulers'] = [s_v8_roll, s_v8_stable, s_v8_beta]

builders = []

# ----------------------------------------------------------------------------
# FACTORIES

m_annotator = annotator_factory.AnnotatorFactory()

# ----------------------------------------------------------------------------
# BUILDER DEFINITIONS

CATEGORY_STABLE = '1Stable|release'
CATEGORY_STABLE_MIPS = '1Stable|release|mips'
CATEGORY_STABLE_PPC = '1Stable|release|ppc'
CATEGORY_BETA = '2Beta|release'
CATEGORY_BETA_MIPS = '2Beta|release|mips'
CATEGORY_BETA_PPC = '2Beta|release|ppc'
CATEGORY_ROLL = '3Roll|roll'

b_v8_linux_roll = {
  'name': 'V8 Linux - roll branch',
  'builddir': 'v8-linux-roll',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_ROLL,
  'auto_reboot' : False,
}

b_v8_linux_roll_debug = {
  'name': 'V8 Linux - roll branch - debug',
  'builddir': 'v8-linux-roll-debug',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_ROLL,
  'auto_reboot' : False,
}

b_v8_linux_stable = {
  'name': 'V8 Linux - stable branch',
  'builddir': 'v8-linux-stable',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_STABLE,
  'auto_reboot' : False,
}

b_v8_linux_stable_debug = {
  'name': 'V8 Linux - stable branch - debug',
  'builddir': 'v8-linux-stable-debug',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_STABLE,
  'auto_reboot' : False,
}

b_v8_linux_beta = {
  'name': 'V8 Linux - beta branch',
  'builddir': 'v8-linux-beta',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_BETA,
  'auto_reboot' : False,
}

b_v8_linux_beta_debug = {
  'name':'V8 Linux - beta branch - debug',
  'builddir': 'v8-linux-beta-debug',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_BETA,
  'auto_reboot' : False,
}

b_v8_linux64_roll = {
  'name': 'V8 Linux64 - roll branch',
  'builddir': 'v8-linux64-roll',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_ROLL,
  'auto_reboot' : False,
}

b_v8_linux64_roll_debug = {
  'name': 'V8 Linux64 - roll branch - debug',
  'builddir': 'v8-linux64-roll-debug',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_ROLL,
  'auto_reboot' : False,
}

b_v8_linux64_stable = {
  'name': 'V8 Linux64 - stable branch',
  'builddir': 'v8-linux64-stable',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_STABLE,
  'auto_reboot' : False,
}

b_v8_linux64_stable_debug = {
  'name': 'V8 Linux64 - stable branch - debug',
  'builddir': 'v8-linux64-stable-debug',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_STABLE,
  'auto_reboot' : False,
}

b_v8_linux64_beta = {
  'name': 'V8 Linux64 - beta branch',
  'builddir': 'v8-linux64-beta',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_BETA,
  'auto_reboot' : False,
}

b_v8_linux64_beta_debug = {
  'name': 'V8 Linux64 - beta branch - debug',
  'builddir': 'v8-linux64-beta-debug',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_BETA,
  'auto_reboot' : False,
}

b_v8_arm_roll = {
  'name': 'V8 arm - sim - roll branch',
  'builddir': 'v8-arm-roll',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_ROLL,
  'auto_reboot' : False,
}

b_v8_arm_roll_debug = {
  'name': 'V8 arm - sim - roll branch - debug',
  'builddir': 'v8-arm-roll-debug',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_ROLL,
  'auto_reboot' : False,
}

b_v8_arm_stable = {
  'name': 'V8 arm - sim - stable branch',
  'builddir': 'v8-arm-stable',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_STABLE,
  'auto_reboot' : False,
}

b_v8_arm_stable_debug = {
  'name': 'V8 arm - sim - stable branch - debug',
  'builddir': 'v8-arm-stable-debug',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_STABLE,
  'auto_reboot' : False,
}

b_v8_arm_beta = {
  'name': 'V8 arm - sim - beta branch',
  'builddir': 'v8-arm-beta',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_BETA,
  'auto_reboot' : False,
}

b_v8_arm_beta_debug = {
  'name': 'V8 arm - sim - beta branch - debug',
  'builddir': 'v8-arm-beta-debug',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_BETA,
  'auto_reboot' : False,
}

b_v8_mipsel_stable = {
  'name': 'V8 mipsel - sim - stable branch',
  'builddir': 'v8-mipsel-stable',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_STABLE_MIPS,
  'auto_reboot' : False,
}

b_v8_mipsel_beta = {
  'name': 'V8 mipsel - sim - beta branch',
  'builddir': 'v8-mipsel-beta',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_BETA_MIPS,
  'auto_reboot' : False,
}

b_v8_mips64el_stable = {
  'name': 'V8 mips64el - sim - stable branch',
  'builddir': 'v8-mips64el-stable',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_STABLE_MIPS,
  'auto_reboot' : False,
}

b_v8_mips64el_beta = {
  'name': 'V8 mips64el - sim - beta branch',
  'builddir': 'v8-mips64el-beta',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_BETA_MIPS,
  'auto_reboot' : False,
}

b_v8_ppc_stable = {
  'name': 'V8 ppc - sim - stable branch',
  'builddir': 'v8-ppc-stable',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_STABLE_PPC,
  'auto_reboot' : False,
}

b_v8_ppc_beta = {
  'name': 'V8 ppc - sim - beta branch',
  'builddir': 'v8-ppc-beta',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_BETA_PPC,
  'auto_reboot' : False,
}

b_v8_ppc64_stable = {
  'name': 'V8 ppc64 - sim - stable branch',
  'builddir': 'v8-ppc64-stable',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_STABLE_PPC,
  'auto_reboot' : False,
}

b_v8_ppc64_beta = {
  'name': 'V8 ppc64 - sim - beta branch',
  'builddir': 'v8-ppc64-beta',
  'factory': m_annotator.BaseFactory('v8'),
  'category': CATEGORY_BETA_PPC,
  'auto_reboot' : False,
}

c['builders'] = [b_v8_linux_stable,
                 b_v8_linux_stable_debug,
                 b_v8_linux64_stable,
                 b_v8_linux64_stable_debug,
                 b_v8_arm_stable,
                 b_v8_arm_stable_debug,
                 b_v8_mipsel_stable,
                 b_v8_mips64el_stable,
                 b_v8_ppc_stable,
                 b_v8_ppc64_stable,
                 b_v8_linux_beta,
                 b_v8_linux_beta_debug,
                 b_v8_linux64_beta,
                 b_v8_linux64_beta_debug,
                 b_v8_arm_beta,
                 b_v8_arm_beta_debug,
                 b_v8_mipsel_beta,
                 b_v8_mips64el_beta,
                 b_v8_ppc_beta,
                 b_v8_ppc64_beta,
                 b_v8_linux_roll,
                 b_v8_linux_roll_debug,
                 b_v8_linux64_roll,
                 b_v8_linux64_roll_debug,
                 b_v8_arm_roll,
                 b_v8_arm_roll_debug]

# Associate the slaves to the builders. The configuration is in slaves.cfg.
slaves = slaves_list.SlavesList('slaves.cfg', 'V8Branches')
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])


####### BUILDSLAVES

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)


####### STATUS TARGETS

# Buildbot master url:
# Must come before AutoSetupMaster().
c['buildbotURL'] = ActiveMaster.buildbot_url

# Adds common status and tools to this master.
master_utils.AutoSetupMaster(c, ActiveMaster,
    public_html='../master.chromium/public_html',
    tagComparator=comparator,
    templates=['../master.client.v8/templates',
               '../master.chromium/templates'])

if MAIL_NOTIFIER:
  import mail_notifier_cfg
  mail_notifier_cfg.Update(config, ActiveMaster, c)

# Adjust the buildCaches to be 3x the number of slaves per builder.
c['autoBuildCacheRatio'] = 3

####### PROJECT IDENTITY

c['projectName'] = ActiveMaster.project_name
c['projectURL'] = config.Master.project_url
